package poc.gridfs;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

	/**
	 * <p>Class       : test.encoder.EncoderThread
	 * <p>Descdription: 验证Encoder对CPU的耗费情况
	 * @author  顾力行-gulixing@msn.com
	 * @version 1.0.0
	 */
	public class CountThread extends Thread {
		public int sum[];
		static int all = 0;
		public Long start = 0L;
		List<GridFSThread> threadList = new ArrayList();
		
		public void add(GridFSThread t){
			this.threadList.add(t);
		}
		
		private void sum(int sum[]){
		    all = 0;
		    for (int i = 0; i < sum.length; i++) {
                  all+=sum[i];
            }
//		    return all;
		}
		
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(CountThread.class);
		    
	
	    @Override
	    public void run() { 
	    	logger.info("============ begin ===========");
	    	sum = new int[threadList.size()];
	    	start = new Date().getTime();
	    	Long point = 1L;
	    	while(true){
	    		try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	    	int i=0;
	    	for (Iterator iterator = threadList.iterator(); iterator.hasNext();) {
				GridFSThread gridFSThread = (GridFSThread) iterator.next();
				sum[i] = gridFSThread.count;
//				logger.info("sum["+i+"]:{}",sum[i]);
				i++;
			}
	    	sum(sum);
	    	point = new Date().getTime();
	    	double avg = all/((double)(point-start)/1000);
	    	logger.info("running {} seconds",(double)(point-start)/1000);
	    	logger.info("{} records inserted, average: {}/s ",all,avg);
	    	}
	    }	
}

